package com.bdqn.bookshop.controller.mall;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.bookshop.entity.Books;
import com.bdqn.bookshop.service.BooksService;
import com.bdqn.bookshop.utils.Result;
import com.bdqn.bookshop.vo.BooksVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author : 罗若然[LUO_RUO_RAN]
 * @date : 2020-08-02 15:55
 **/
@Controller
@RequestMapping("/books")
public class BooksController {

    @Autowired
    private BooksService booksService;

    @RequestMapping("/user/editBooks")
    @ResponseBody
    public Result addBooks(@RequestParam(name="upload",required=false) MultipartFile file, Books books, HttpServletRequest request){
        return booksService.editBooks(file,books,request);
    }

    @RequestMapping("/user/booksListCount/{representative}")
    @ResponseBody
    public Result booksListCount(HttpServletRequest request,@PathVariable("representative") String representative){
        return booksService.booksListCount(request,representative);
    }
    @RequestMapping("/booksListCount")
    @ResponseBody
    public Result booksListCount(Books books){
        return  booksService.booksListCount(books);
    }

    @RequestMapping("/user/booksList/{representative}")
    public String booksList(BooksVo booksVo, HttpServletRequest request, Model model,@PathVariable("representative") String representative){
        Page<Books> booksPage = booksService.booksList(booksVo, request,representative);
        model.addAttribute("booksList",booksPage.getRecords());
        if(representative.equals("publisher")) {
            return "mall/published::data";
        }else{
            return "mall/bought::data";
        }
    }

    @RequestMapping("/booksList")
    public String booksList(BooksVo booksVo,Model model){
        List<Books> books = (List<Books>) booksService.bookList(booksVo);
        model.addAttribute("booksList",books);
        return "mall/list::dataList";
    }

    @RequestMapping("/classifyBooksList")
    public String classifyBooksList(Integer id,Model model){
        model.addAttribute("booksList",booksService.getBooksByClassifyID(id));
        return "mall/index::dataList";
    }

    @RequestMapping("/editBookState")
    @ResponseBody
    public Result editBookState(Books books){
        return booksService.updateBookState(books);
    }
}
